from django.db import models

class Category(models.Model):
    name = models.CharField(max_length=50, unique=True, verbose_name='分类名称')
    sort_order = models.IntegerField(default=0, verbose_name='排序')
    image = models.ImageField(upload_to='categories/', blank=True, null=True, verbose_name='分类图片')
    is_active = models.BooleanField(default=True, verbose_name='是否启用')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        ordering = ['sort_order', 'id']
        verbose_name = '商品分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Product(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='商品分类')
    name = models.CharField(max_length=100, verbose_name='商品名称')
    description = models.TextField(blank=True, verbose_name='商品描述')
    image = models.ImageField(upload_to='products/', blank=True, null=True, verbose_name='商品图片')
    is_active = models.BooleanField(default=True, verbose_name='是否上架')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        ordering = ['-created_at']
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class SKU(models.Model):
    product = models.ForeignKey(Product, related_name='skus', on_delete=models.CASCADE, verbose_name='商品')
    name = models.CharField(max_length=50, verbose_name='规格名称')
    image = models.ImageField(upload_to='skus/', blank=True, null=True, verbose_name='规格图片')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    stock = models.PositiveIntegerField(default=0, verbose_name='库存')
    is_active = models.BooleanField(default=True, verbose_name='是否启用')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        ordering = ['id']
        verbose_name = '商品规格'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.product.name} - {self.name}'

class Banner(models.Model):
    title = models.CharField(max_length=100, verbose_name='标题')
    image = models.ImageField(upload_to='banners/', verbose_name='轮播图片')
    link = models.CharField(max_length=200, blank=True, null=True, verbose_name='链接地址')
    sort_order = models.IntegerField(default=0, verbose_name='排序')
    is_active = models.BooleanField(default=True, verbose_name='是否启用')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '轮播图'
        verbose_name_plural = verbose_name
        ordering = ['sort_order']

    def __str__(self):
        return self.title 